package com.cxb;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.media.AudioManager;
import android.os.Handler;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import com.cxb.manager.CrashHandler;
import com.cxb.manager.PhoneManager;
import com.cxb.receiver.PowerStateReceiver;
import com.cxb.utils.CommonUtils;
import com.cxb.utils.SharedPreferencesUtils;
import org.xutils.x;

public class CXBApplication extends Application {
	PowerStateReceiver powerStateReceiver;
	private SharedPreferencesUtils spUtils;
	private AudioManager mAudioManager;
	private PhoneManager mPhoneManager;
	public static long bootTime = 0;
	private static String TAG = "CXBApplication";
	private Handler mHandler;

	@Override
	public void onCreate() {

		try {
			x.Ext.init(this);
			x.Ext.setDebug(false);
			bootTime = System.currentTimeMillis();
			if (powerStateReceiver == null) {
				powerStateReceiver = new PowerStateReceiver();
			}
			registerReceiver(powerStateReceiver, new IntentFilter(
					Intent.ACTION_BATTERY_CHANGED));
			System.loadLibrary("cxb");
			Settings.System.putInt(this.getContentResolver(),
					"gprs_transfer_setting", 0);// GPRS优先

			if (!x.isDebug()) {
				Settings.Secure.putInt(getContentResolver(), "adb_enabled",   0);
			} else {
				Settings.Secure.putInt(getContentResolver(), "adb_enabled", 1);
			}

			spUtils = new SharedPreferencesUtils(this);
			mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

			Settings.System.putInt(getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS, spUtils.getBrightness());

			int volumn = spUtils.getLastVolumn();
			if (volumn >= 0) {
				mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
						volumn, 0);
				mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,
						volumn, 0);
				mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM,
						volumn, 0);
				mAudioManager.setStreamVolume(AudioManager.STREAM_RING, volumn,
						0);
				mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
				spUtils.setLastVoulmn(-1);

			}
			Settings.System.putInt(getContentResolver(),
					Settings.System.AUTO_TIME, 0);
			Settings.System.putInt(getContentResolver(), "auto_time_gps", 1);
			try {
				int a = Settings.System.getInt(getContentResolver(),
						"auto_time_gps");
				System.out.println(a);
			} catch (SettingNotFoundException e) {

				e.printStackTrace();
			}
			Settings.Secure.setLocationProviderEnabled(getContentResolver(),
					LocationManager.GPS_PROVIDER, true);
			// Settings.Secure.putInt(getContentResolver(), "location_mode", 1);

			if (!x.isDebug()) {
				CrashHandler crashHandler = CrashHandler.getInstance();
				crashHandler.init(getApplicationContext());

			}



		} catch (Exception e) {
			e.printStackTrace();
		}

		mPhoneManager = new PhoneManager(this);
		mHandler = new Handler();



		super.onCreate();
		CommonUtils.setDefaultDataSIM(this,1);
		CommonUtils.setMobileData(this, 1,true);
	}


}
